package javagprssrv.DataAnalyse;

import java.net.InetAddress;
import java.net.DatagramPacket;
import java.nio.charset.Charset;
import javagprssrv.StructClass.structTerm;
import javagprssrv.StructClass.structTCPServerSocket;
import javagprssrv.Common.CommonClass;
import java.nio.ByteBuffer;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.StringWriter;
import DT42.TrdTCPDataTrans_DT42;
import DT8.TrdTCPDataTrans_DT8;
import NBJL.TrdTCPDataTrans_NBJL;
import DT3.TrdUDPDataTrans;
import javagprssrv.JGS_Param;
import java.net.DatagramSocket;
import javagprssrv.StructClass.structRecvData;

public class TrdDataAnalyse extends java.lang.Thread {

    javagprssrv.StructClass.structRecvData recvData;
    java.net.DatagramSocket myDS;
    javagprssrv.DataAnalyse.ScriptExecuter ScriptExecuter;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    private boolean isSendUDPDataMustByServerSocket;

    public TrdDataAnalyse() {
        super("TrdDataAnalyse");
        this.RecvDataType = -1;
        this.isSendUDPDataMustByServerSocket = true;
        this.recvData = new javagprssrv.StructClass.structRecvData();
        try {
            this.myDS = new java.net.DatagramSocket();
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    @java.lang.Override
    public void run() {
        try {

            while (true) {
                Thread.sleep(20L);

                boolean isGetData = false;
                if (JGS_Param.UnDoneRecvDatas.size() > 0) {
                    isGetData = true;
                    this.recvData = ((structRecvData) JGS_Param.UnDoneRecvDatas
                            .remove(0));
                }
                if (isGetData) {
                    this._Protocol = this.recvData.Protocol.trim();
                    this._RemoteIP = this.recvData.RemoteIP;
                    this._RemotePort = this.recvData.RemotePort;
                    this._UDPServerSocketIndex = this.recvData.UDPServerSocketIndex;
                    this._Data = this.recvData.Data;
                    this._OriginRecvData = this.recvData.Data;
                    if (this._Protocol.equals("UDP")) {
                        new TrdUDPDataTrans(this.recvData).start();
                    } else if ((this._Protocol.equals("TCP"))
                            && (this._Data.substring(0, 1).equals("A"))) {
                        new TrdTCPDataTrans(this.recvData).start();
                    } else if ((this._Protocol.equals("TCP"))
                            && (this._Data.substring(0, 1).equals(String
                                    .valueOf('$')))) {
                        new TrdTCPDataTrans_NBJL(this.recvData).start();
                    } else if ((this._Protocol.equals("TCP"))
                            && (this._Data.substring(0, 1).equals(String
                                    .valueOf('\006')))) {
                        new TrdTCPDataTrans_NBJL(this.recvData).start();
                    } else if ((this._Protocol.equals("TCP"))
                            && (this._Data.charAt(0) == '¥')) {
                        this.recvData.Data = ("\003000" + this.recvData.Data
                                + '' + '\000');

                        new TrdTCPDataTrans_DT8(this.recvData).start();
                    } else if ((this._Protocol.equals("TCP"))
                            && (this._Data.charAt(0) == 'h')) {
                        new TrdTCPDataTrans_DT42(this.recvData).start();
                    } else if (this._Protocol.equals("TCP")) {
                        new TrdTCPDataTrans_DT8(this.recvData).start();
                    } else {
                    }
                }
            }
        } catch (Exception e) {

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            String errlog = pw.toString();
            String err = "[ TrdDataAnalyse.run()异常: recvData.IP="
                    + this.recvData.RemoteIP + " , recvData.Port="
                    + String.valueOf(this.recvData.RemotePort) + " ] ";
            JGS_Param.myLog.ShowLog(err + errlog, Boolean.valueOf(true),
                    Boolean.valueOf(true));
        }

    }

    public void TCPBroadcast(final java.lang.String strBC) {
        java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(strBC.length());
        for (int i = 0; i < javagprssrv.JGS_Param.TCPServerSockets.size(); ++i) {
            try {
                bb = javagprssrv.Common.CommonClass.String2ByteBuffer(strBC);
                ((structTCPServerSocket) javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).ServerChannel
                        .write(bb);
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }

    public javagprssrv.StructClass.structTCPServerSocket getCurrentTCPServerSocket(
            final java.lang.String RemoteIP, final int RemotePort) {
        javagprssrv.StructClass.structTCPServerSocket rt = null;
        boolean isFind = false;
        for (int i = 0; i < javagprssrv.JGS_Param.TCPServerSockets.size(); ++i) {
            rt = ((structTCPServerSocket) javagprssrv.JGS_Param.TCPServerSockets.elementAt(i));
            if (rt.RemoteIP.equals(RemoteIP) && rt.RemotePort == RemotePort) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            rt = null;
        }
        return rt;
    }

    public void ServerSendData(final java.lang.String strSend) {
        if (this._Protocol.equals("TCP")) {
            this.TCPDataSend(strSend);
        }
        if (this._Protocol.equals("UDP")) {
            this.UDPDataSend(strSend);
        }
    }

    public void LoginTerms() {
        if (this._Protocol.equals("TCP") && this.RecvDataType == 4) {
            java.lang.String _XZQYM = "";
            java.lang.String _TermNo = "";
            _XZQYM = this._OriginRecvData.substring(7, 9);
            _TermNo = this._OriginRecvData.substring(1, 4);
            final java.lang.String ASYNString = _XZQYM + _TermNo;
            boolean isFindTerm = java.lang.Boolean.FALSE;
            for (int i = 0; i <= javagprssrv.JGS_Param.Terms.size() - 1; ++i) {
                if (javagprssrv.JGS_Param.Terms.elementAt(i).Term_AsynString
                        .equals(ASYNString)) {
                    isFindTerm = java.lang.Boolean.TRUE;
                    break;
                }
            }
            if (!isFindTerm) {
                javagprssrv.StructClass.structTerm sTerm = new javagprssrv.StructClass.structTerm();
                sTerm.Term_AsynString = ASYNString;
                sTerm.Term_WorkProtocol = "TCP";
                sTerm.Term_RemoteIP = this._RemoteIP;
                sTerm.Term_TermNo = java.lang.Integer.valueOf(_TermNo);
                sTerm.Term_RemotePort = this._RemotePort;
                sTerm.Term_ActivateType = -1;
                synchronized (javagprssrv.JGS_Param.Terms) {
                    javagprssrv.JGS_Param.Terms.add(sTerm);
                }
                sTerm = null;
            }
        }
    }

    private void TCPDataSend(final java.lang.String strSend) {
        try {
            this.getCurrentTCPServerSocket(this._RemoteIP, this._RemotePort).ServerChannel
                    .write(java.nio.charset.Charset.forName("ISO-8859-1")
                            .encode(strSend));
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    private void UDPDataSend(final java.lang.String strSend) {
        try {
            final java.net.DatagramPacket DP = new java.net.DatagramPacket(
                    strSend.getBytes(), strSend.getBytes().length);
            DP.setAddress(java.net.InetAddress.getByName(this._RemoteIP));
            DP.setPort(this._RemotePort);
            final byte[] buf = javagprssrv.Common.CommonClass
                    .String2ByteArray(strSend);
            DP.setData(buf, 0, strSend.length());
            if (this.isSendUDPDataMustByServerSocket) {
                ((DatagramSocket) javagprssrv.JGS_Param.UDPServerSockets.elementAt(
                        this._UDPServerSocketIndex)).send(DP);
            } else {
                this.myDS.send(DP);
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
